from django.shortcuts import render

from django.http import HttpResponse,JsonResponse

from .. models import Users,Order,OrderInfo
from django.contrib.auth.decorators import permission_required



#订单页面
@permission_required('myadmin.show_order',raise_exception=True)

def orderlists(request):
    
    # 获取数据
    orderlists = Order.objects.filter()

    orders = request.GET.get('type',None)

    if orders:

        keywords = request.GET.get('keywords',None)

        

        data = {orders+'__contains':keywords}
        orderlists = orderlists.filter(**data)

    pageindex = request.GET.get('p',1)
    # 导入分页类
    from django.core.paginator import Paginator
    # 实例化分页对象
    p = Paginator(orderlists, 3)

    # 获取当前页的 数据
    orderlists = p.page(pageindex)

    con = {'orderlists':orderlists}



    return render(request,'admin/order/orderlists.html',con)

#订单详情
@permission_required('myadmin.show_order',raise_exception=True)

def orderinfo(request,oid):
    
    #获取数据
    obs = OrderInfo.objects.filter(orderid=oid)

    con = {'oinfo':obs}

    return render(request,'admin/order/orderinfo.html',con)

#发货事件
@permission_required('myadmin.show_orders',raise_exception=True)

def orderfahuo(request):

    #获取id
    fid = request.GET['fid']
    try:
        #查询数据
        ob = Order.objects.get(id=fid)
        ob.status = 2
        ob.save()
        return JsonResponse({'code':0,'msg':'已发货，待收货'})
    except:
        return JsonResponse({'code':1,'msg':'发货失败'})


    


